package com.example.goodpet.my.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.goodpet.R;
import com.example.goodpet.base.BaseFragmentForView;
import com.example.goodpet.bean.ResponseBody;
import com.example.goodpet.bean.UserPetInfo;
import com.example.goodpet.bean.UserPetInfoAndHistory;
import com.example.goodpet.common.EndlessRecyclerOnScrollListener;
import com.example.goodpet.common.LoadMoreWrapper;
import com.example.goodpet.common.LoadingPage;
import com.example.goodpet.db.DatabaseHelper;
import com.example.goodpet.db.dao.GoodpetHistoryDAO;
import com.example.goodpet.db.entity.GoodpetHistory;
import com.example.goodpet.my.activity.MyHistoryActivity;
import com.example.goodpet.my.adapter.PetHistoryAdapter;
import com.example.goodpet.service.UserService;
import com.example.goodpet.utils.MyApplication;
import com.example.goodpet.utils.RetrofitSingleton;
import com.example.goodpet.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class MyHistoryPetFragment extends BaseFragmentForView {
    private RecyclerView recyclerView;

    private List<UserPetInfoAndHistory> data;
    private Retrofit retrofit;
    private UserService userService;
    private GoodpetHistoryDAO goodpetHistoryDAO;

    private LoadMoreWrapper loadMoreWrapper;
    private List<GoodpetHistory> goodpetHistories;
    private int size;


    @Override
    protected void LoadPage() {
        loadingPage.setStateCurrent(LoadingPage.STATE_loading);
        userService.getPetHistory(goodpetHistories.subList(0,Math.min(size,10))).enqueue(new Callback<ResponseBody<List<UserPetInfoAndHistory>>>() {
            @Override
            public void onResponse(Call<ResponseBody<List<UserPetInfoAndHistory>>> call, Response<ResponseBody<List<UserPetInfoAndHistory>>> response) {
                ResponseBody<List<UserPetInfoAndHistory>> body = response.body();
                if (body!=null){
                    if (body.getCode()==200){
                        loadingPage.setStateCurrent(LoadingPage.STATE_SUCCESS);
                        data=body.getData();
                        //Log.d(TAG, "onResponse: "+data.toString());
                        //Log.d(TAG, "onResponse: "+);
                        PetHistoryAdapter petHistoryAdapter = new PetHistoryAdapter(data, getActivity());
                        loadMoreWrapper = new LoadMoreWrapper(petHistoryAdapter);
                        recyclerView.setAdapter(loadMoreWrapper);
                        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),1));
                        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener() {
                            @Override
                            public void onLoadMore() {
                                loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING);
                                int begin = (++loadMoreWrapper.page - 1) * MyApplication.PAGE_SIZE;
                                //int end=begin+MyApplication.PAGE_SIZE;
                                if (!(begin >= size )){
                                    userService.getPetHistory(goodpetHistories.subList(begin,Math.min(10,Math.abs(size-begin)))).enqueue(new Callback<ResponseBody<List<UserPetInfoAndHistory>>>() {
                                        @Override
                                        public void onResponse(Call<ResponseBody<List<UserPetInfoAndHistory>>> call, Response<ResponseBody<List<UserPetInfoAndHistory>>> response) {
                                            ResponseBody<List<UserPetInfoAndHistory>> body1 = response.body();
                                            //Log.d(TAG, "onResponse: "+ body);
                                            if (body1.getCode() == 200) {
                                                loadingPage.setStateCurrent(LoadingPage.STATE_SUCCESS);
                                                List<UserPetInfoAndHistory> data1 = body1.getData();
                                                data.addAll(data1);

                                                loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING_COMPLETE);
                                            } else if (body1.getCode() == 404) {
                                                loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING_END);
                                            }

                                        }

                                        @Override
                                        public void onFailure(Call<ResponseBody<List<UserPetInfoAndHistory>>> call, Throwable t) {
                                            t.printStackTrace();
                                            UIUtils.toast("连接服务器失败", false);
                                        }
                                    });
                                }else {
                                    loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING_END);
                                }
                            }
                        });

                    }else if (body.getCode()==404){
                        loadingPage.setStateCurrent(LoadingPage.STATE_EMPTY);
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody<List<UserPetInfoAndHistory>>> call, Throwable t) {
                t.printStackTrace();
                //UIUtils.toast("连接服务器失败",false);
                loadingPage.setStateCurrent(LoadingPage.STATE_ERROR);
            }
        });
    }
    @Override
    protected void initView(View inflate) {
        super.initView(inflate);
        recyclerView=inflate.findViewById(R.id.recyclerView);
    }

    @Override
    public void initListener() {
        super.initListener();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_user_info_rv;
    }

    @Override
    protected void initData() {
        goodpetHistoryDAO=DatabaseHelper.getInstance(getContext()).getGoodpetHistoryDAO();
        data=new ArrayList<>();
        goodpetHistories = goodpetHistoryDAO.selectListHistory(MyApplication.user.getId());
        retrofit= RetrofitSingleton.getRetrofitNetConfig();
        userService=retrofit.create(UserService.class);
        size = goodpetHistories.size();
    }

    public void clear(){
        int len=data.size();
        if (len!=0){
        data.clear();
        loadMoreWrapper.notifyItemRangeRemoved(0,len);
        }
    }

}
